package logic

import (
	"context"
	"novel-sys/goods/goods"
	"novel-sys/pkg"
	"strconv"

	"novel-sys/api/internal/svc"
	"novel-sys/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginRegisterApiLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginRegisterApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginRegisterApiLogic {
	return &LoginRegisterApiLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginRegisterApiLogic) LoginRegisterApi(req *types.LoginRegisterRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	register, err := l.svcCtx.GoodsRpc.LoginRegister(l.ctx, &goods.LoginRegisterRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		Sms:      req.Sms,
		Type:     req.Type,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(register.UserId, 10))
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Message: "登录成功",
		Data:    handler,
		Code:    200,
	}, nil
}
